BlockList get custom data from Controller
# help-with-umbraco
p
I wanted to ask what would possibly be the best way to get data from examine inside a block. My code looks like the following: TopArticleBlock which should get the data from the ArticleController (method: GetTopArticle). What would be the cleanest way now to get to this data? Should i create a ViewComponent? What else could i do to call the here: @using Umbraco.Cms.Core.Models @using MD.Ornis.Ornis.Umb.Controllers @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Umbraco.Cms.Core.Models.Blocks.BlockGridItem> <<<<>>>>>>> @* Top Artikel *@
d
ViewComponent would be a very clean solution for your use case and very appropriate as well. If you don't want to use a viewcomponent, I'd recommend creating a service class in C#, register it in the DI container and then inject it into your view like this:
Copy code
@inject IMySearchService mySearchService
@{
    var myData = mySearchService.GetMyData(Model.SomeParameterThatINeed)
}
You should not call methods on any controller from your view code. That's the old way of doing things. If you need that logic in your controller, you should refactor the logic into a separate service that you can inject into your controller and into your view.
p
@D_Inventor Thank you for your response! You've helped me alot! In the mean time i've tried the ViewComponent aproach and it works really well! Again thank you alot!
d
Glad I could help 😊
2 Views